package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentEvaluate;
import com.mxpio.erp.common.equipment.service.EquipmentEvaluateService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentEvaluateController", description = "评价列表")
@RestController
@RequestMapping("/erp/equipment/evaluate/")
public class EquipmentEvaluateController {

    @Autowired
    private EquipmentEvaluateService equipmentEvaluateService;


    @GetMapping("list")
    @Operation(summary = "评价列表", description = "获取评价列表（无分页）", method = "GET")
    public Result<List<EquipmentEvaluate>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentEvaluate> list = equipmentEvaluateService.list(EquipmentEvaluate.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "评价列表", description = "获取评价列表列表（分页）", method = "GET")
    public Result<Page<EquipmentEvaluate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentEvaluate> pageResult = equipmentEvaluateService.listPage(EquipmentEvaluate.class, page, criteria);
        return Result.OK(pageResult);
    }

    @RequestMapping("add")
    @Operation(summary = "添加并保存评价信息", description = "添加并保存评价信息")
    public Result<EquipmentEvaluate> add(@RequestBody EquipmentEvaluate equipmentEvaluate) {
        equipmentEvaluateService.save(equipmentEvaluate,new SmartCrudPolicyAdapter(){
//            @Override
//            public boolean beforeInsert (CrudContext context){
//                Object o = context.getEntity();
//                if (o instanceof EquipmentEvaluateDetail){
//                    EquipmentEvaluate parent = context.getParent();
//                    ((EquipmentEvaluateDetail) o).setDetailId(parent.getId());
//                }
//                return true;
//            }
        });
        return Result.OK(equipmentEvaluate);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑评价", description = "编辑评价", method = "PUT")
    public Result<EquipmentEvaluate> edit(@RequestBody EquipmentEvaluate equipmentEvaluate) {
        equipmentEvaluateService.update(equipmentEvaluate);
        return Result.OK(equipmentEvaluate);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除评价表", description = "删除设备SOP,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentEvaluate> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentEvaluateService.delete(EquipmentEvaluate.class, key);
        }
        return Result.OK();
    }
}
